package com.yjzx.util.common.util.map;

import java.util.Map;
import java.util.Objects;

/**
 * @author guozhi
 * @date 2024/12/24
 * @description MyEntry
 */
public interface MyEntry<K, V> extends Map.Entry<K, V> {


    MyEntry EMPTY = new MyEntry() {
        @Override
        public boolean isClear() {
            return true;
        }

        @Override
        public Map.Entry getEntry() {
            return null;
        }

        @Override
        public Object getKey() {
            return null;
        }

        @Override
        public Object getValue() {
            return null;
        }

        @Override
        public Object setValue(Object value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o || o == null) {
                return true;
            }
            if (o instanceof Map.Entry) {
                Map.Entry v = ((Map.Entry) o);
                return v.getKey() == null && v.getValue() == null;
            }
            return false;
        }
    };

    /**
     * 空的entry
     * @return
     */
    static <K, V>MyEntry<K, V> empty() {
        return (MyEntry<K, V>)EMPTY;
    }

    /**
     * 原始的map entry
     * @return
     */
    Map.Entry<K, V> getEntry();

    /**
     * 判断是否需要清理
     *
     * @return
     */
    default boolean isClear() {
        return getValue() == null;
    }


    static <K, V> MyEntry<K, V> of(Map.Entry<K, V> entry) {
        if (entry instanceof MyEntry) {
            return (MyEntry<K, V>) entry;
        } else {
            return entry == null ? empty() : new MyEntry<K, V>() {

                @Override
                public Map.Entry<K, V> getEntry() {
                    return entry;
                }

                @Override
                public K getKey() {
                    return entry.getKey();
                }

                @Override
                public V getValue() {
                    return entry.getValue();
                }

                @Override
                public V setValue(V value) {
                    return value;
                }

                @Override
                public boolean equals(Object o) {
                    if (this == o) {
                        return true;
                    }
                    if (!(o instanceof Map.Entry)) {
                        return false;
                    }
                    Map.Entry v = ((Map.Entry) o);
                    return Objects.equals(entry.getKey(), v.getValue()) &&
                            Objects.equals(entry.getValue(), v.getValue());
                }

                @Override
                public int hashCode() {
                    return entry.hashCode();
                }

                @Override
                public String toString() {
                    return "MyEntry{" +
                            "key=" + getKey() +
                            ",value=" + getValue() +
                            '}';
                }
            };
        }
    }

}
